package com.hongfeng.support.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.OrderInfo;
import com.hongfeng.support.service.OrderInfoService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <订单信息Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class OrderInfoController extends BaseController {

	@Resource
	private OrderInfoService orderInfoService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String orderNo, Page<OrderInfo> page) {
		log.info("订单号：" + orderNo);
		ParmItem item = new ParmItem("orderNo", orderNo, FieldOperator.LIKE);
		orderInfoService.findPageByQuery(page, OrderInfo.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("orderInfo") String orderInfoJson) {
		log.info("添加订单信息:" + (orderInfoJson != null ? orderInfoJson : null));
		if (orderInfoJson == null) {
			this.sendErrorResponse(response, "订单信息不能为空。");
			return;
		}
		OrderInfo orderInfo = (OrderInfo) JsonUtils.json2Object(orderInfoJson,
				OrderInfo.class);
		ParmItem item = new ParmItem("orderNo", orderInfo.getOrderNo(),
				FieldOperator.EQUAL);
		List<OrderInfo> list = orderInfoService.findAllByQuery(OrderInfo.class,
				item);
		if (list.size() == 0) {
			orderInfoService.create(orderInfo);
		} else {
			this.sendErrorResponse(response, "该订单号已存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("orderInfo") String orderInfoJson) {
		log.info("修改订单信息:" + (orderInfoJson != null ? orderInfoJson : null));
		if (orderInfoJson == null) {
			this.sendErrorResponse(response, "订单信息不能为空。");
			return;
		}
		OrderInfo orderInfo = (OrderInfo) JsonUtils.json2Object(orderInfoJson,
				OrderInfo.class);
		ParmItem item = new ParmItem("orderNo", orderInfo.getOrderNo(),
				FieldOperator.EQUAL);
		List<OrderInfo> list = orderInfoService.findAllByQuery(OrderInfo.class,
				item);
		if (list.size() == 0) {
			OrderInfo qOrderInfo = orderInfoService.read(orderInfo.getId());
			orderInfo.setCreateTime(qOrderInfo.getCreateTime());
			orderInfo.setCreator(qOrderInfo.getCreator());
			orderInfoService.update(orderInfo);
		} else {
			this.sendErrorResponse(response, "该订单号已存在");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取订单信息:" + id);
		OrderInfo orderInfo = orderInfoService.read(id);
		String json = JsonUtils.bean2Json(orderInfo);
		log.info("订单信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除订单信息:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的订单信息。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			orderInfoService.delete(Long.parseLong(id));
		}
	}
}
